package com.sam.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

public class FootNavBar extends LinearLayout {
    private Context context;
    private Drawable oneDrawable;
    private Drawable twoDrawable;
    private Drawable threeDrawable;
    private Drawable fourDrawable;
    private String oneTextView,twoTextView,threeTextView,fourTextView;
    private int footerpadding,footerTextcolr;
    private float footerTextSize;
    private String TextFontPathInAssets;
    private FontTextView oneItem,twoItem,threeItem,fourTtem;
    private FontTextView[] items;

    public FootNavBar(Context context) {
        this(context,null);
    }

    public FootNavBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FootNavBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        getAttrs(attrs);
        findview();
        initview();
    }
    private void getAttrs(AttributeSet attrs) {
        TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.FootNavBar);
        oneDrawable=ta.getDrawable(R.styleable.FootNavBar_oneDrawable);
        twoDrawable=ta.getDrawable(R.styleable.FootNavBar_twoDrawable);
        threeDrawable=ta.getDrawable(R.styleable.FootNavBar_threeDrawable);
        fourDrawable=ta.getDrawable(R.styleable.FootNavBar_fourDrawable);
        oneTextView=ta.getString(R.styleable.FootNavBar_oneText);
        twoTextView=ta.getString(R.styleable.FootNavBar_twoText);
        threeTextView=ta.getString(R.styleable.FootNavBar_threeText);
        fourTextView=ta.getString(R.styleable.FootNavBar_fourText);
        footerpadding= ta.getDimensionPixelSize(R.styleable.FootNavBar_footerpadding,0);
        footerTextSize=ta.getDimension(R.styleable.FootNavBar_footerTextSize,0f);
        footerTextcolr=ta.getColor(R.styleable.FootNavBar_footerTextColor, Color.WHITE);
        TextFontPathInAssets=ta.getString(R.styleable.FootNavBar_itemTextFontPathInAssets);
        ta.recycle();
    }
    private void findview() {
        View view=View.inflate(context,R.layout.widget_foot_nav_bar,this);
        oneItem= (FontTextView) view.findViewById(R.id.nav_tab1);
        twoItem= (FontTextView) view.findViewById(R.id.nav_tab2);
        threeItem= (FontTextView) view.findViewById(R.id.nav_tab3);
        fourTtem= (FontTextView) view.findViewById(R.id.nav_tab4);
        items=new FontTextView[]{oneItem,twoItem,threeItem,fourTtem};
    }

    private void initview() {
        if(TextFontPathInAssets != null){
            try {
                Typeface tf = Typeface.createFromAsset(context.getAssets(),TextFontPathInAssets);
                oneItem.setTypeface(tf);
                twoItem.setTypeface(tf);
                threeItem.setTypeface(tf);
                fourTtem.setTypeface(tf);
            }catch (Exception e){}

        }
        oneDrawable.setBounds(0,0,oneDrawable.getMinimumWidth(),oneDrawable.getMinimumHeight());
        twoDrawable.setBounds(0,0,twoDrawable.getMinimumWidth(),twoDrawable.getMinimumHeight());
        threeDrawable.setBounds(0,0,threeDrawable.getMinimumWidth(),threeDrawable.getMinimumHeight());
        fourDrawable.setBounds(0,0,fourDrawable.getMinimumWidth(),fourDrawable.getMinimumHeight());

        oneItem.setCompoundDrawables(null,oneDrawable,null,null);
        twoItem.setCompoundDrawables(null,twoDrawable,null,null);
        threeItem.setCompoundDrawables(null,threeDrawable,null,null);
        fourTtem.setCompoundDrawables(null,fourDrawable,null,null);

        oneItem.setCompoundDrawablePadding(footerpadding);
        twoItem.setCompoundDrawablePadding(footerpadding);
        threeItem.setCompoundDrawablePadding(footerpadding);
        fourTtem.setCompoundDrawablePadding(footerpadding);
        oneItem.setText(oneTextView);
        twoItem.setText(twoTextView);
        threeItem.setText(threeTextView);
        fourTtem.setText(fourTextView);
        oneItem.setTextSize(footerTextSize);
        twoItem.setTextSize(footerTextSize);
        threeItem.setTextSize(footerTextSize);
        fourTtem.setTextSize(footerTextSize);
        oneItem.setTextColor(footerTextcolr);
        twoItem.setTextColor(footerTextcolr);
        threeItem.setTextColor(footerTextcolr);
        fourTtem.setTextColor(footerTextcolr);

        oneItem.setOnClickListener(onClickListener);
        twoItem.setOnClickListener(onClickListener);
        threeItem.setOnClickListener(onClickListener);
        fourTtem.setOnClickListener(onClickListener);
    }

    private int currentPosition = -1;
    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(onNavItemChangedListener == null) return;
            int i = v.getId();
            if (i == R.id.nav_tab1) {
                if (currentPosition != 0) {
                    onNavItemChangedListener.onNavItemChanged(items, oneItem, 0);
                    currentPosition = 0;
                }

            } else if (i == R.id.nav_tab2) {
                if (currentPosition != 1) {
                    onNavItemChangedListener.onNavItemChanged(items, twoItem, 1);
                    currentPosition = 1;
                }

            } else if (i == R.id.nav_tab3) {
                if (currentPosition != 2) {
                    onNavItemChangedListener.onNavItemChanged(items, threeItem, 2);
                    currentPosition = 2;
                }

            } else if (i == R.id.nav_tab4) {
                if (currentPosition != 3) {
                    onNavItemChangedListener.onNavItemChanged(items, fourTtem, 3);
                    currentPosition = 3;
                }

            }
        }
    };

    private OnNavItemChangedListener onNavItemChangedListener;

    public interface OnNavItemChangedListener{
        void onNavItemChanged(FontTextView[] items, FontTextView item, int posistion);
    }

    public void setOnNavItemChangedListener(OnNavItemChangedListener onNavItemChangedListener) {
        this.onNavItemChangedListener = onNavItemChangedListener;
    }

    }



